﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JDML.Attributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
    public class FieldAttribute : Attribute
    {
        private bool isMapping = true;
        private bool primaryKey = false;
        private bool autoGenerate = false;
        public string FieldName { get; set; }
        public bool PrimaryKey
        {
            get
            {
                return primaryKey;
            }
            set
            {
                primaryKey = value;
                autoGenerate = primaryKey ? true : false;
            }
        }
        public bool AutoGenerate {
            get 
            { 
                return autoGenerate;
            }
            set 
            { 
                if (value && (!primaryKey)) 
                { 
                    throw new Exception("只有Primary Key 才能表示为Identity自动生成。");
                }
                autoGenerate = value; 
            } 
        }
        public bool IsMapping { get { return isMapping; } set { isMapping = value; } }
        public FieldAttribute()
        {
        }
        public FieldAttribute(string fieldName, bool primaryKey = false, bool autoGenerate = false)
        {
            FieldName = fieldName;
            PrimaryKey = primaryKey;
            AutoGenerate = autoGenerate;
        }
    }
}
